package com.yixueyun.learning.service.Impl;

import com.yixueyun.base.model.RestResponse;
import com.yixueyun.content.model.po.CoursePublish;
import com.yixueyun.learning.feignclient.ContentServiceClient;
import com.yixueyun.learning.feignclient.MediaServiceClient;
import com.yixueyun.learning.model.dto.YxyCourseTablesDto;
import com.yixueyun.learning.service.LearningService;
import com.yixueyun.learning.service.MyCourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class LearningServiceImpl implements LearningService {
    @Autowired
    private MyCourseTablesService myCourseTablesService;
    @Autowired
    private MediaServiceClient mediaServiceClient;
    @Autowired
    private ContentServiceClient contentServiceClient;
    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if(coursepublish == null) {
            return RestResponse.validfail("课程不存在");
        }
        //判断是否支持试学

        //判断是否又学习资格
        //如果登录
        if(StringUtils.isNotEmpty(userId)){
            //判断是否选课，根据选课情况判断学习资格
            YxyCourseTablesDto yxyCourseTablesDto = myCourseTablesService.getLearningStatus(userId, courseId);
            //学习资格状态 [{"code":"702001","desc":"正常学习"},{"code":"702002","desc":"没有选课或选课后没有支付"},{"code":"702003","desc":"已过期需要申请续期或重新支付"}]
            String learnStatus = yxyCourseTablesDto.getLearnStatus();
            if(learnStatus.equals("702002")){
                return RestResponse.validfail("无法学习，因为没有选课或者没有支付");
            }else if(learnStatus.equals("702003")){
                return RestResponse.validfail("您的选课已过期需要申请续期或重新支付");
            } else {
                //有资格学习
                //远程调用媒资
                return mediaServiceClient.getPlayUrlByMediaId(mediaId);
            }
        }
        //未登录或未选课判断是否收费
        String charge = coursepublish.getCharge();
        if(charge.equals("201000")){//免费可以正常学习
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }

        return RestResponse.validfail("请购买课程后继续学习");

    }
}
